package com.group01.web.controller;

import com.group01.annotation.Order;
import com.group01.dto.Order.OrderDTO;
import com.group01.enums.CodeAndMessageEnum;
import com.group01.result.Result;
import com.group01.service.OrderService;
import com.group01.vo.Order.OrderGetInfoVO;
import com.group01.vo.Order.OrderGetPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/23
 */
@RestController
@RequestMapping("/order")
@Validated
@Api(tags = "订单模块")
public class OrderController {
    /**
     * 注入
     */
    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 获取用户的订单列表
     * @param id 订单编号
     * @param status 订单状态
     * @return {@link Result }<{@link OrderGetInfoVO }>
     */
    @ApiOperation("获取用户的订单列表")
    @GetMapping
    public Result<List<OrderGetPageVO>> getOrderInfo(
            @RequestParam(value = "id" , required = false) String id,
            @RequestParam(value = "status" , required = false) @Order Integer status
    ) {
        return orderService.getOrderInfos(id, status);
    }

    /**
     * 根据顶单编号查询订单信息
     * @param orderId 订单编号
     * @return {@link Result }<{@link OrderGetInfoVO }>
     */
    @ApiOperation("根据顶单编号查询订单信息")
    @GetMapping("/{orderId}")
    public Result<OrderGetInfoVO> getOrderInfo(@PathVariable("orderId") @NotEmpty(message = "订单编号不能为空") String orderId) {
        return Result.success(CodeAndMessageEnum.FIND_RESULT, orderService.getOrderInfoById(orderId));
    }

    /**
     * 新增订单
     * @param orderDTO 订单信息
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("新增订单")
    @PostMapping
    public Result<OrderGetPageVO> addOrder(@RequestBody @NotNull(message = "订单信息不能为空") OrderDTO orderDTO) {
        return orderService.addOrder(orderDTO);
    }

    /**
     * 确认收货
     * @param orderId 订单编号
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("确认收货")
    @PutMapping("/complete/{orderId}")
    public Result<String> completeOrder(@PathVariable("orderId") @NotEmpty(message = "订单编号不能为空") String orderId) {
        return orderService.completeOrder(orderId);
    }

    /**
     * 关闭订单
     * @param orderId 订单编号
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("关闭订单")
    @PutMapping("/close/{orderId}")
    public Result<String> closeOrder(@PathVariable("orderId") @NotEmpty(message = "订单编号不能为空") String orderId) {
        return orderService.closeOrder(orderId);
    }
}
